<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/22
 * Time: 11:26
 */

namespace app\index\controller;


use app\admin\model\News as NewsModel;
use app\admin\model\NewsCate;
use think\captcha\Captcha;
use think\Controller;

class Commen extends Controller
{
    /**
     * 初始化
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function initialize()
    {
        //  首页第一级别栏目-------战区栏目    与普通类栏目分来开  分别查出每个分类的6条新闻
        $where = [];
        $where[] = ['is_del','=',1];
        $where[] = ['cate_id','=',2];
        $zhanQuCateList = NewsCate::getFieldValue('id,cate_name',$where,'true',['sort'=>'asc']);

        foreach ($zhanQuCateList as $k => $v)
        {
            $where = [];
            $where[] = ['cate_id','=',$v['id']];
            $zhanQuCateList[$k]['newsList'] = NewsModel::getLimitList($where,'id,title,abstract,cate_id,event_image',['create_time'=>'desc'],'0','7');
        }
        $this->assign("zhanQuCateList",$zhanQuCateList);

        //  首页第一级别栏目-------普通类栏目
        $where = [];
        $where[] = ['is_del','=',1];
        $where[] = ['cate_id','=',1];
        $puTongList = NewsCate::getFieldValue('id,cate_name',$where,'true',['sort'=>'asc']);
        foreach ($puTongList as $k => $v)
        {
            $offect = 0;
            $limit = 5;

            $where = [];
            $where[] = ['cate_id','=',$v['id']];
            if($v['id'] == 14 || $v['id'] == 10)
            {
                $offect = 0;
                $limit = 8;
            }
            $puTongList[$k]['newsList'] = NewsModel::getLimitList($where,'id,title,abstract,cate_id,event_image',['create_time'=>'desc'],$offect,$limit);
        }
        $this->assign("puTongList",$puTongList);
    }
    /**
     * 获取分页参数
     * @param $page
     * @param $pageSize
     * @return mixed
     */
    public function getPageAndPageSize($page,$pageSize){
        $pageOption['page'] = $page?$page:1;;
        $pageOption['pageSize'] = $pageSize?$pageSize:config('pageSize');
        return $pageOption;
    }

    /**
     * 验证码
     * @return \think\Response
     */
    public function verify(){
    $config =    [
        // 验证码字体大小
        'fontSize'    =>    25,
        // 验证码位数
        'length'      =>    4,
        // 关闭验证码杂点
        'useNoise'    =>    false,
        'bg'          =>    [255,255,255],
    ];
    $captcha = new Captcha($config);
    return $captcha->entry();
}
}